﻿using System;
using UnityEngine;

// Token: 0x0200022F RID: 559
public class PlayerCamera : MonoBehaviour, IListener
{
	// Token: 0x17000160 RID: 352
	// (get) Token: 0x06000BBA RID: 3002 RVA: 0x00061188 File Offset: 0x0005F388
	// (set) Token: 0x06000BBB RID: 3003 RVA: 0x00061190 File Offset: 0x0005F390
	public bool firstPersonMode { get; private set; }

	// Token: 0x06000BBC RID: 3004 RVA: 0x0006119C File Offset: 0x0005F39C
	public void OnNotify(IEvent e)
	{
		StepEvent stepEvent = (StepEvent)e;
		float f = stepEvent.entity.Height / this.parentTransform.localScale.y * stepEvent.magnitude;
		float num = Mathf.Log10(f);
		float num2 = stepEvent.entity.Height * num;
		if (num < 0f)
		{
			return;
		}
		float num3 = (stepEvent.position - base.transform.position).magnitude;
		num3 = 1f - num3 / num2;
		num3 = Mathf.Clamp01(num3);
		this.Shake(num * num3);
	}

	// Token: 0x06000BBD RID: 3005 RVA: 0x0006123C File Offset: 0x0005F43C
	private void Awake()
	{
		this.parentTransform = base.transform.parent;
		PlayerCamera.defaultFOV = GlobalPreferences.Fov.value;
		this.collisionLayer = Layers.cameraCollisionMask;
		PlayerCamera.instance = this;
		this.mainCamera = base.GetComponent<Camera>();
		this.farCamera = base.transform.GetChild(0).GetComponent<Camera>();
		this.camEffectSettingsScript = base.gameObject.GetComponent<CameraEffectsSettings>();
		this.vr = base.GetComponent<VRCamera>();
		PlayerCamera.shakeMultipler = GlobalPreferences.ShakeMultiplier.value;
		this.orbit.mouseSensibility = GlobalPreferences.MouseSensibility.value;
	}

	// Token: 0x06000BBE RID: 3006 RVA: 0x000612E0 File Offset: 0x0005F4E0
	private void Start()
	{
		this.targetScale = GameController.startingSize;
		this.SetCameraTarget(this.target);
		this.MoveToTarget();
		this.position.distanceFromTarget = this.position.distanceFromTarget * this.targetScale;
		this.position.adjustementDistance = -this.position.distanceFromTarget;
		EventManager.Register(this, EventCode.OnStep);
	}

	// Token: 0x06000BBF RID: 3007 RVA: 0x0006134C File Offset: 0x0005F54C
	public void SetCameraTarget(Transform t)
	{
		this.target = t;
		if (this.target != null)
		{
			this.entity = this.target.GetComponent<EntityBase>();
			if (this.entity == null)
			{
				return;
			}
			if (this.entity.isGiantess)
			{
				if (this.entity.isPlayer)
				{
					this.player = t.Find("Player Character").GetComponent<Player>();
				}
				this.collisionLayer = Layers.gtsWalkableMask;
				return;
			}
			if (this.target.GetComponent<VehicleEnterExit>())
			{
				this.collisionLayer = Layers.vehicleCameraCollisionMask;
				return;
			}
			this.collisionLayer = Layers.cameraCollisionMask;
			Player component = this.target.GetComponent<Player>();
			if (component == null)
			{
				component = this.target.GetComponent<MicroControl>();
			}
			if (component)
			{
				this.player = component;
				this.resizeChar = this.target.GetComponent<ResizeCharacter>();
			}
		}
	}

	// Token: 0x06000BC0 RID: 3008 RVA: 0x0006144C File Offset: 0x0005F64C
	public void SwitchShooterMode(bool enable)
	{
		if (enable)
		{
			this.SetCameraTarget(this.player.shootCameraTarget.transform);
			this.previousTargetZoom = this.position.targetZoom;
			this.position.targetZoom = -2.5f;
			this.position.smooth = 3f;
			this.entity = this.player;
		}
		else
		{
			this.SetCameraTarget(this.player.transform);
			this.position.targetZoom = this.previousTargetZoom;
			this.position.smooth = 2f;
		}
		this.shooterMode = enable;
	}

	// Token: 0x06000BC1 RID: 3009 RVA: 0x000614F0 File Offset: 0x0005F6F0
	private void Update()
	{
		this.GetInput();
	}

	// Token: 0x06000BC2 RID: 3010 RVA: 0x000614F8 File Offset: 0x0005F6F8
	private void LateUpdate()
	{
		if (GameController.Instance.paused || this.player == null)
		{
			return;
		}
		this.AdjustToPlayerSize();
		this.UpdateFOV();
		this.OrbitTarget();
		this.MoveToTarget();
		this.LookAtTarget();
		this.ZoomInOnTarget();
		this.CollisionDetection();
		this.ShakeEffect();
	}

	// Token: 0x06000BC3 RID: 3011 RVA: 0x00061558 File Offset: 0x0005F758
	private void AdjustToPlayerSize()
	{
		if (this.entity != null)
		{
			this.targetScale = this.entity.AccurateScale;
			this.parentTransform.localScale = new Vector3(this.targetScale, this.targetScale, this.targetScale);
			this.mainCamera.nearClipPlane = this.camEffectSettingsScript.defaultNearPlane * this.targetScale;
		}
	}

	// Token: 0x06000BC4 RID: 3012 RVA: 0x000615C8 File Offset: 0x0005F7C8
	private void CollisionDetection()
	{
		Vector3 vector = this.destination - this.targetPos;
		float num = -this.position.distanceFromTarget;
		Debug.DrawLine(this.targetPos, this.targetPos + vector, Color.green);
		RaycastHit raycastHit;
		this.colliding = Physics.Raycast(this.targetPos, vector, out raycastHit, vector.magnitude * 1.5f, this.collisionLayer);
		float num2 = raycastHit.distance * 0.9f;
		float num3 = 15f;
		float num4 = this.WhiskerCollisionCheck(vector, -num3, 0f);
		float num5 = this.WhiskerCollisionCheck(vector, num3, 0f);
		float num6 = this.WhiskerCollisionCheck(vector, 0f, -num3);
		float num7 = this.WhiskerCollisionCheck(vector, 0f, num3);
		float num8 = Mathf.Min(new float[]
		{
			num4,
			num5,
			num6,
			num7,
			num
		});
		float num9 = num8;
		float num10 = 0.5f;
		if (this.colliding && num2 < num9)
		{
			num9 = num2;
			num10 = 16f;
		}
		if (num8 < this.position.adjustementDistance)
		{
			num10 = 8f;
		}
		num9 *= 0.9f;
		this.position.adjustementDistance = Mathf.Lerp(this.position.adjustementDistance, num9, num10 * Time.deltaTime);
	}

	// Token: 0x06000BC5 RID: 3013 RVA: 0x00061728 File Offset: 0x0005F928
	private float WhiskerCollisionCheck(Vector3 direction, float x_rotation, float y_rotation)
	{
		Quaternion quaternion = Quaternion.LookRotation(direction);
		quaternion *= Quaternion.Euler(x_rotation, y_rotation, 0f);
		direction = quaternion * Vector3.forward * direction.magnitude;
		RaycastHit raycastHit;
		if (Physics.Raycast(this.targetPos, direction, out raycastHit, direction.magnitude * 1.5f, this.collisionLayer))
		{
			Debug.DrawLine(this.targetPos, this.targetPos + direction, Color.red);
			return raycastHit.distance;
		}
		Debug.DrawLine(this.targetPos, this.targetPos + direction, Color.green);
		return -this.position.distanceFromTarget;
	}

	// Token: 0x06000BC6 RID: 3014 RVA: 0x000617E0 File Offset: 0x0005F9E0
	private void GetInput()
	{
		float timeScale = Time.timeScale;
		if (Input.GetButtonDown(ButtonInput.CameraSwitch))
		{
			this.firstPersonMode = !this.firstPersonMode;
			this.player.Render(!this.firstPersonMode);
			this.player.SwitchPerspective();
		}
		if (!this.shooterMode)
		{
			this.middleMouseClick = Input.GetButton(ButtonInput.LookBack);
		}
		this.rawInputH = Input.GetAxis(ButtonInput.MouseX);
		this.hOrbitInput = this.rawInputH * this.orbit.mouseSensibility * timeScale;
		this.vOrbitInput = Input.GetAxis(ButtonInput.MouseY) * this.orbit.mouseSensibility * timeScale;
		if (this.shooterMode)
		{
			return;
		}
		if (Input.GetKey(KeyCode.LeftShift))
		{
			this.heightModifier += Input.GetAxisRaw(ButtonInput.MouseScrollWheel);
			this.heightModifier = Mathf.Clamp(this.heightModifier, 0.1f, 1.3f);
		}
		else
		{
			this.zoomInput = Input.GetAxisRaw(ButtonInput.MouseScrollWheel);
		}
		if (Input.GetButton(ButtonInput.ZoomIn))
		{
			this.zoomInput += 0.1f;
		}
		else if (Input.GetButton(ButtonInput.ZoomOut))
		{
			this.zoomInput -= 0.1f;
		}
	}

	// Token: 0x06000BC7 RID: 3015 RVA: 0x0006193C File Offset: 0x0005FB3C
	private void MoveToTarget()
	{
		if (this.player == null)
		{
			return;
		}
		float xRotation = this.orbit.xRotation;
		float num = this.orbit.yRotation;
		if (this.middleMouseClick)
		{
			num += 180f;
		}
		Quaternion quaternion;
		if (this.player.gameObject.activeSelf)
		{
			quaternion = Quaternion.LookRotation(this.player.referenceTransform.forward, this.player.referenceTransform.up);
		}
		else
		{
			quaternion = Quaternion.Euler(0f, 0f, 0f);
		}
		float num2 = PlayerCamera.defaultHeight;
		Vector3 b = Vector3.up;
		if (this.vr.IsInVR())
		{
			num2 = 1f;
		}
		else if (this.entity != null && this.entity.isGiantess)
		{
			num2 = 0.9f;
		}
		else if (this.player.isClimbing())
		{
			if (this.firstPersonMode)
			{
				b = 0.35f * this.player.myTransform.up + 0.4f * this.player.myTransform.forward;
			}
			else
			{
				b = 0.35f * this.player.myTransform.up + 0.05f * this.player.myTransform.forward;
			}
		}
		else if (this.player.isCrushed)
		{
			if (this.firstPersonMode)
			{
				b = 0.1f * this.player.myTransform.up - 0.45f * this.player.myTransform.forward;
			}
			else if (this.player.IsStuck())
			{
				b = 0.2f * this.player.myTransform.up - 0f * this.player.myTransform.forward;
			}
			else
			{
				b = 0.1f * this.player.myTransform.up - 0f * this.player.myTransform.forward;
			}
		}
		num2 *= this.heightModifier;
		this.position.up = Vector3.Lerp(this.position.up, b, Time.deltaTime * 4f);
		this.position.verticalOffset = Mathf.Lerp(this.position.verticalOffset, num2, Time.deltaTime * 4f);
		this.targetPos = this.target.position + this.position.up * this.position.verticalOffset * this.targetScale;
		if (this.firstPersonMode && this.entity != null && this.entity.isGiantess)
		{
			Vector3 forward = base.transform.forward;
			forward.y = 0f;
			forward.Normalize();
			this.targetPos = this.entity.GetEyesPosition() + forward * (this.forwardView * this.targetScale);
		}
		if (!this.player.isClimbing() && this.player.gameObject.activeSelf && this.wasClimbing)
		{
			Quaternion quaternion2;
			if (this.firstPersonMode)
			{
				quaternion2 = this.player.transform.rotation * Quaternion.Euler(xRotation, 180f, 0f);
			}
			else
			{
				quaternion2 = this.player.transform.rotation * Quaternion.Euler(xRotation, num, 0f);
			}
			num = quaternion2.eulerAngles.y;
			this.wasClimbing = false;
		}
		Quaternion rotation = quaternion;
		Quaternion rotation2 = Quaternion.Euler(xRotation, num, 0f);
		float num3 = -this.position.distanceFromTarget;
		if (this.firstPersonMode)
		{
			num3 = 0.02f * this.targetScale;
		}
		this.destination = rotation * (rotation2 * Vector3.forward) * num3;
		this.destination += this.targetPos;
		this.parentTransform.position = this.destination;
		float adjustementDistance = this.position.adjustementDistance;
		if (!this.firstPersonMode && adjustementDistance < num3)
		{
			num3 = adjustementDistance;
			this.adjustedDestination = rotation * (rotation2 * Vector3.forward) * num3;
			this.adjustedDestination += this.targetPos;
			this.parentTransform.position = this.adjustedDestination;
		}
	}

	// Token: 0x06000BC8 RID: 3016 RVA: 0x00061E4C File Offset: 0x0006004C
	private void LookAtTarget()
	{
		Quaternion rotation;
		if (this.player.gameObject.activeSelf)
		{
			rotation = Quaternion.LookRotation(this.targetPos - this.parentTransform.position, this.player.referenceTransform.up);
		}
		else
		{
			rotation = Quaternion.LookRotation(this.targetPos - this.parentTransform.position);
		}
		this.parentTransform.rotation = rotation;
	}

	// Token: 0x06000BC9 RID: 3017 RVA: 0x00061EC8 File Offset: 0x000600C8
	private void OrbitTarget()
	{
		if (this.firstPersonMode && this.vr.IsInVR())
		{
			this.orbit.xRotation = 0f;
		}
		else
		{
			this.orbit.xRotation += this.vOrbitInput;
		}
		this.orbit.yRotation += this.hOrbitInput;
		this.orbit.xRotation = Mathf.Clamp(this.orbit.xRotation, this.orbit.minXRotation, this.orbit.maxXRotation);
	}

	// Token: 0x06000BCA RID: 3018 RVA: 0x00061F68 File Offset: 0x00060168
	private void ZoomInOnTarget()
	{
		this.position.targetZoom *= 1f - this.zoomInput * this.position.zoomSmooth * Time.deltaTime;
		this.position.targetZoom = Mathf.Clamp(this.position.targetZoom, this.position.maxZoom, this.position.minZoom);
		float distanceFromTarget = this.position.distanceFromTarget;
		this.position.distanceFromTarget = Mathf.Lerp(distanceFromTarget, this.position.targetZoom * this.targetScale, this.position.smooth * Time.deltaTime);
		this.position.adjustementDistance *= this.position.distanceFromTarget / distanceFromTarget;
	}

	// Token: 0x06000BCB RID: 3019 RVA: 0x00062038 File Offset: 0x00060238
	private void ShakeEffect()
	{
		if (this.vr.IsInVR())
		{
			return;
		}
		if (this.shakeStrength > 0f)
		{
			Vector3 vector = UnityEngine.Random.onUnitSphere * this.shakeStrength;
			vector.z = 0f;
			vector *= this.targetScale;
			this.parentTransform.position = Vector3.Lerp(this.parentTransform.position, this.parentTransform.position + vector, 0.5f * Time.deltaTime);
			this.shakeStrength -= PlayerCamera.shakeMultipler * Time.deltaTime;
		}
	}

	// Token: 0x06000BCC RID: 3020 RVA: 0x000620E0 File Offset: 0x000602E0
	public void Shake(float intensity)
	{
		if (this.vr.IsInVR())
		{
			return;
		}
		float num = intensity * PlayerCamera.shakeMultipler;
		if (num > this.shakeStrength)
		{
			this.shakeStrength = num;
		}
	}

	// Token: 0x06000BCD RID: 3021 RVA: 0x0006211C File Offset: 0x0006031C
	private void UpdateFOV()
	{
		if (this.vr.IsInVR())
		{
			return;
		}
		this.targetFOV = PlayerCamera.defaultFOV;
		float num = 2f;
		if (!this.firstPersonMode)
		{
			if (this.player.isSprinting())
			{
				this.targetFOV += this.sprintFOV;
			}
			else if (this.orbit.xRotation > 0f)
			{
				float num2 = this.orbit.xRotation / this.orbit.maxXRotation;
				num = num2 * 20f;
				if (num < 2f)
				{
					num = 2f;
				}
				this.targetFOV += this.orbit.xRotation / this.orbit.maxXRotation * this.lookDownFOV;
			}
		}
		if (this.targetFOV > 110f)
		{
			this.targetFOV = 110f;
		}
		float fieldOfView = Mathf.Lerp(this.mainCamera.fieldOfView, this.targetFOV, Time.deltaTime * num);
		this.mainCamera.fieldOfView = fieldOfView;
		this.farCamera.fieldOfView = fieldOfView;
	}

	// Token: 0x06000BCE RID: 3022 RVA: 0x00062240 File Offset: 0x00060440
	public void SetMouseSensibility(float sensibility)
	{
		this.orbit.mouseSensibility = sensibility;
		GlobalPreferences.MouseSensibility.value = sensibility;
	}

	// Token: 0x06000BCF RID: 3023 RVA: 0x00062259 File Offset: 0x00060459
	public Camera getMainCamera()
	{
		return this.mainCamera;
	}

	// Token: 0x040010E3 RID: 4323
	private CameraEffectsSettings camEffectSettingsScript;

	// Token: 0x040010E4 RID: 4324
	public float smoothRotation = 10f;

	// Token: 0x040010E5 RID: 4325
	public static float defaultHeight = 1.35f;

	// Token: 0x040010E6 RID: 4326
	public static PlayerCamera instance;

	// Token: 0x040010E7 RID: 4327
	private Camera farCamera;

	// Token: 0x040010E8 RID: 4328
	public Transform target;

	// Token: 0x040010E9 RID: 4329
	public EntityBase entity;

	// Token: 0x040010EA RID: 4330
	public float forwardView = 0.14f;

	// Token: 0x040010EB RID: 4331
	private bool colliding;

	// Token: 0x040010EC RID: 4332
	public PlayerCamera.PositionSettings position = new PlayerCamera.PositionSettings();

	// Token: 0x040010ED RID: 4333
	public PlayerCamera.OrbitSettings orbit = new PlayerCamera.OrbitSettings();

	// Token: 0x040010EE RID: 4334
	private Vector3 targetPos = Vector3.zero;

	// Token: 0x040010EF RID: 4335
	private Vector3 destination = Vector3.zero;

	// Token: 0x040010F0 RID: 4336
	private Vector3 adjustedDestination = Vector3.zero;

	// Token: 0x040010F1 RID: 4337
	private Player player;

	// Token: 0x040010F2 RID: 4338
	private float hOrbitInput;

	// Token: 0x040010F3 RID: 4339
	private float vOrbitInput;

	// Token: 0x040010F4 RID: 4340
	private float zoomInput;

	// Token: 0x040010F5 RID: 4341
	private float hOrbitSnapInput;

	// Token: 0x040010F6 RID: 4342
	private float heightModifier = 1f;

	// Token: 0x040010F8 RID: 4344
	private bool shooterMode;

	// Token: 0x040010F9 RID: 4345
	private float previousTargetZoom;

	// Token: 0x040010FA RID: 4346
	private bool wasClimbing;

	// Token: 0x040010FB RID: 4347
	public float rawInputH;

	// Token: 0x040010FC RID: 4348
	public float targetScale = 1f;

	// Token: 0x040010FD RID: 4349
	public static float defaultFOV = 65f;

	// Token: 0x040010FE RID: 4350
	public float lookDownFOV = 15f;

	// Token: 0x040010FF RID: 4351
	public float sprintFOV = 25f;

	// Token: 0x04001100 RID: 4352
	private bool middleMouseClick;

	// Token: 0x04001101 RID: 4353
	private float targetFOV;

	// Token: 0x04001102 RID: 4354
	private Camera mainCamera;

	// Token: 0x04001103 RID: 4355
	public static float shakeMultipler;

	// Token: 0x04001104 RID: 4356
	public float shakeStrength;

	// Token: 0x04001105 RID: 4357
	public ResizeCharacter resizeChar;

	// Token: 0x04001106 RID: 4358
	public LayerMask collisionLayer;

	// Token: 0x04001107 RID: 4359
	private Transform parentTransform;

	// Token: 0x04001108 RID: 4360
	private VRCamera vr;

	// Token: 0x02000230 RID: 560
	[Serializable]
	public class PositionSettings
	{
		// Token: 0x04001109 RID: 4361
		public Vector3 up = Vector3.up;

		// Token: 0x0400110A RID: 4362
		public Vector3 forward = Vector3.forward;

		// Token: 0x0400110B RID: 4363
		public float verticalOffset = 1.5f;

		// Token: 0x0400110C RID: 4364
		public float targetZoom = -2.5f;

		// Token: 0x0400110D RID: 4365
		public float distanceFromTarget = -2.5f;

		// Token: 0x0400110E RID: 4366
		public float zoomSmooth = 0.1f;

		// Token: 0x0400110F RID: 4367
		public float minZoom = -2f;

		// Token: 0x04001110 RID: 4368
		public float maxZoom = -200f;

		// Token: 0x04001111 RID: 4369
		public float smooth = 2f;

		// Token: 0x04001112 RID: 4370
		[HideInInspector]
		public float adjustementDistance = 9999f;
	}

	// Token: 0x02000231 RID: 561
	[Serializable]
	public class OrbitSettings
	{
		// Token: 0x04001113 RID: 4371
		public float xRotation = -20f;

		// Token: 0x04001114 RID: 4372
		public float yRotation = -180f;

		// Token: 0x04001115 RID: 4373
		public float maxXRotation = 85f;

		// Token: 0x04001116 RID: 4374
		public float minXRotation = -85f;

		// Token: 0x04001117 RID: 4375
		public float mouseSensibility = 8f;
	}
}
